<?php

/**
 * Page object for our template manager
 *
 * @author Michael Peacock
 * @version 1.0
 */
class page {


	// page elements

	// page title
	private $title = '';
	// template tags
	private $tags = array();
	// tags which should be processed after the page has been parsed
	// reason: what if there are template tags within the database content, we must parse the page, then parse it again for post parse tags
	private $postParseTags = array();
	// template bits
	private $bits = array();
	// the page content
	private $content = '';

	private $apd =  array();

	/**
	 * Create our page object
	 */
	function __construct( Registry $registry )
	{
		$this->registry = $registry;
	}

	/**
	 * Get the page title from the page
	 * @return String
	 */
	public function getTitle()
	{
		return $this->title;
	}

	/**
	 * Set the page title
	 * @param String $title the page title
	 * @return void
	 */
	public function setTitle( $title )
	{
		$this->title = $title;
	}

	/**
	 * Set the page content
	 * @param String $content the page content
	 * @return void
	 */
	public function setContent( $content )
	{
		$this->content = $content;
	}

	/**
	 * Add a template tag, and its replacement value/data to the page
	 * @param String $key the key to store within the tags array
	 * @param String $data the replacement data (may also be an array)
	 * @return void
	 */
	public function addTag( $key, $data )
	{
		$this->tags[$key] = $data;
	}

	public function removeTag( $key )
	{
		unset( $this->tags[$key] );
	}

	/**
	 * Get tags associated with the page
	 * @return void
	 */
	public function getTags()
	{
		return $this->tags;
	}

	/**
	 * Add post parse tags: as per adding tags
	 * @param String $key the key to store within the array
	 * @param String $data the replacement data
	 * @return void
	 */
	public function addPPTag( $key, $data )
	{
		$this->postParseTags[$key] = $data;
	}

	/**
	 * Get tags to be parsed after the first batch have been parsed
	 * @return array
	 */
	public function getPPTags()
	{
		return $this->postParseTags;
	}

	/**
	 * Add a template bit to the page, doesnt actually add the content just yet
	 * @param String the tag where the template is added
	 * @param String the template file name
	 * @return void
	 */
	public function addTemplateBit( $tag, $bit, $replacements=array() )
	{
		$this->bits[ $tag ] = array( 'template' => $bit, 'replacements' => $replacements);
	}

	/**
	 * Adds additional parsing data
	 * A.P.D is used in parsing loops.  We may want to have an extra bit of data depending on on iterations value
	 * for example on a form list, we may want a specific item to be "selected"
	 * @param String block the condition applies to
	 * @param String tag within the block the condition applies to
	 * @param String condition : what the tag must equal
	 * @param String extratag : if the tag value = condition then we have an extra tag called extratag
	 * @param String data : if the tag value = condition then extra tag is replaced with this value
	 */
	public function addAdditionalParsingData($block, $tag, $condition, $extratag, $data)
	{
		$this->apd[$block] = array($tag => array('condition' => $condition, 'tag' => $extratag, 'data' => $data));
	}

	/**
	 * Get the template bits to be entered into the page
	 * @return array the array of template tags and template file names
	 */
	public function getBits()
	{
		return $this->bits;
	}

	public function getAdditionalParsingData()
	{
		return $this->apd;
	}

	/**
	 * Gets a chunk of page content
	 * @param String the tag wrapping the block ( <!-- START tag --> block <!-- END tag --> )
	 * @return String the block of content
	 */
	public function getBlock( $tag )
	{
		preg_match ('#<!-- START '. $tag . ' -->(.+?)<!-- END '. $tag . ' -->#si', $this->content, $tor);
		if (isset($tor[0]))
		{
			$tor = str_replace ('<!-- START '. $tag . ' -->', "", $tor[0]);

			$tor = str_replace ('<!-- END '  . $tag . ' -->', "", $tor);
		}

		return $tor;
	}

	public function getContent()
	{
		return $this->content;
	}

	public function getContentToPrint()
	{
		$this->content = preg_replace ('#{form_(.+?)}#si', '', $this->content);
		$this->content = preg_replace ('#{nbd_(.+?)}#si', '', $this->content);
		$this->content = str_replace('</body>', '<!-- Generated by our Fantastic Social Netowk -->
</body>', $this->content );
    	
    	
	    return $this->content;
    }
}
?>
